package com.yj.suncity.area;

import java.util.List;

import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.bean.ProvinceBean;
import com.yj.suncity.bean.ProvinceListBean;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.ProvinceManager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class CitySelectActivity extends Activity implements OnItemClickListener {

	private ListView listView;
	private Context context;
	private String provinceId;
	private ProvinceManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_area_select);
		manager = ProvinceManager.getInstance();
		provinceId = getIntent().getStringExtra("provinceid");
		context = this;
		findView();
		initData();
	}

	private void findView() {
		((TextView) findViewById(R.id.txt_title_name)).setText("选择城市");
		listView = (ListView) findViewById(R.id.list_area);
		listView.setOnItemClickListener(this);
	}

	private void initData() {
		List<ProvinceBean> provinceBeans = manager.getCity(provinceId);
		if (provinceBeans!=null) {
			listView.setAdapter(new AreaAdapter(provinceBeans));
		} else {
			String path = URLCon.地区 + "?action=citybyssdm&ssdm=" + provinceId;
			AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
					false, null, true, "正在加载......", new ResultCallBack() {
				@Override
				public void JSONResult(JSONObject jsonObject) {
					ProvinceListBean listBean = new ProvinceListBean(
							jsonObject);
					manager.addCity(provinceId, listBean.getDs());
					listView.setAdapter(new AreaAdapter(listBean.getDs()));
				}
			});
			asyncTaskUtil.execute(path);
		}
	}

	class AreaAdapter extends BaseAdapter {
		private List<ProvinceBean> beans;

		public AreaAdapter(List<ProvinceBean> beans) {
			super();
			this.beans = beans;
		}

		@Override
		public int getCount() {
			return beans.size();
		}

		@Override
		public Object getItem(int position) {
			return beans.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.adapter_area_item, null);
				holder.textView = (TextView) convertView
						.findViewById(R.id.txt_area_name);
				holder.view = (ImageView) convertView
						.findViewById(R.id.img_more);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			ProvinceBean provinceBean = beans.get(position);
			holder.textView.setText(provinceBean.getDjsmc());

			return convertView;
		}

	}

	class ViewHolder {
		TextView textView;
		ImageView view;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		ProvinceBean provinceBean = (ProvinceBean) listView.getAdapter()
				.getItem(position);
		Intent intent = new Intent(this, CountySelectActivity.class);
		intent.putExtra("cityid", provinceBean.getDjsdm());
		intent.putExtra("provinceid", provinceId);
		startActivityForResult(intent, 2);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 2 && resultCode == 3) {

			Log.d("",
					data.getStringExtra("allname") + "======="
							+ data.getStringExtra("countyname") + "======="
							+ data.getStringExtra("areaid"));
			Intent intent = new Intent();
			intent.putExtra("allname", data.getStringExtra("allname"));
			intent.putExtra("areaid", data.getStringExtra("areaid"));
			intent.putExtra("countyname", data.getStringExtra("countyname"));
			setResult(1, intent);
			this.finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
